package bai1;


import java.util.Scanner;

public class Rational implements Comparable {

	/**
	 * @param args
	 */
	private double numberator, denominator;
	
	public Rational(double ts, double ms)
	{
		if((ts==0)||(ms==0)) System.out.println("Tu so va mau so phai khac khong");
		this.numberator=ts;
		this.denominator=ms;
		Reduce(this.numberator,this.denominator);
	}
	public Rational()
	{
		this.numberator=0;
		this.denominator=0;
	}
	
	
	public double USCLN(double a, double b)
	{
		if(a==0|| b== 0)
			return a+b;
		else
		{
			while(a!=b)
			{
				if(a>b) a=a-b;
				else b=b-a;
			}
			return a;
		}
	}
	//toi gian
	public void Reduce(double a, double b)
	{
		double i = USCLN(a,b);
		a=a/i;
		b=b/i;
		if(b<0){
			a=-a;
			b=-b;
		}
		System.out.println("Phan so sau khi toi gian: " +a+"/"+b);
		
	}
	//cong
	public Rational Add(Rational ps1, Rational ps2)
	{
		Rational ps = new Rational();
		ps.numberator=ps1.numberator*ps2.denominator+ps1.denominator*ps2.numberator;
		ps.denominator=ps1.denominator*ps2.denominator;
		ps.Reduce(this.numberator,this.denominator);
		return ps;
		
	}
	//public Rational()
	//tru
	public Rational Subtract(Rational ps1, Rational ps2)
	{
		Rational ps = new Rational();
		ps.numberator=ps1.numberator*ps2.denominator-ps1.denominator*ps2.numberator;
		ps.denominator=ps1.denominator*ps2.denominator;
		ps.Reduce(this.numberator,this.denominator);
		return ps;
		
	}
	//nhan
	public Rational Multifly(Rational ps1, Rational ps2)
	{
		Rational ps = new Rational();
		ps.numberator=ps1.numberator*ps2.numberator;
		ps.denominator=ps1.denominator*ps2.denominator;
		ps.Reduce(this.denominator,this.numberator);
		return ps;
	}
	//nghich dao
	public void Reciprocal(double ts, double ms)
	{
		
		if((ts==0)||(ms==0)) System.out.println("Error");
		else
		{
			ts=ms;
			ms=ts;
		}
	}
	//chia
	public Rational Divide(Rational ps1, Rational ps2)
	{
		Rational ps = new Rational();
		ps1.Reciprocal(ps1.numberator,ps1.denominator);
		ps.numberator=ps1.numberator*ps2.numberator;
		ps.denominator=ps1.denominator*ps2.denominator;
		ps.Reduce(this.numberator,this.denominator);
		return ps;
	}
	//so sanh
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	int compareTo(Rational ps2)
	{
		double s1 = this.numberator/this.denominator;
	//	s1= Math.round(s1*1000)/1000;
		double s2 = ps2.numberator/ps2.denominator;
	//	s2=Math.round(s2*1000)/1000;
		if(Math.abs(s1-s2)<0.0001)
			return 0;
		
		else
			return 1;
	}
	@Override
	public int compareTo(Object arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	

}



